package com.fourforfo.fourmall.order.listener;

import com.fourforfo.fourmall.order.entity.OrderEntity;
import com.fourforfo.fourmall.order.feign.MemberFeignService;
import com.fourforfo.fourmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @program: xxl-sso-core
 * @description: 订单取消，订单异常回滚监听器
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-09-11 19:43
 **/
@Slf4j
@Component
@RabbitListener(queues = "order.release.queue")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderCloseListener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try{
            log.info("订单自动关闭监听服务被调用");
            orderService.closeOrder(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
